package com.weipt.juc.syncollector;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;

/**
 * @author peter.wei 2020/09/15 20:17
 * @version 1.0
 * @company 终生学习
 * @description <br>peter.wei 2020/09/15 20:17
 **/
public class TestConcurrentHashMap {

    private static Map<String, String> map = new HashMap<>();

    private static Map<String, String> concurrentMap = new ConcurrentHashMap<>();

    public static void main(String[] args) throws InterruptedException {

        for (int i = 0; i < 100; i++) {
            new Thread(()->{
                TestConcurrentHashMap.putMap();
            }).start();
        }

        for (int i = 0; i < 100; i++) {
            new Thread(()->{
                TestConcurrentHashMap.putConcurrentMap();
            }).start();
        }
        TimeUnit.SECONDS.sleep(1);
        System.out.println(map.size()); //结果每次不一样
        System.out.println(concurrentMap.size());  //结果50
    }


    private static void putMap(){
        for (int i = 0; i < 50; i++) {
            map.put(String.valueOf(i), String.valueOf(i));
        }
    }

    private static void putConcurrentMap(){
        for (int i = 0; i < 50; i++) {
            concurrentMap.put(String.valueOf(i), String.valueOf(i));
        }
    }
}